home *** CD-ROM | disk | FTP | other *** search
- //----------------------------------------------------------------------------------------
- //----------------------------------------------------------------------------------------
- //
- // Filename : gamemain.cpp
- // Description : Member Definitions GameMain class
- // Author : Marnich van Rensburg (2002)
- //
- //----------------------------------------------------------------------------------------
- //----------------------------------------------------------------------------------------
-
-
- #include "gamemain.h"
-
-
- //----------------------------------------------------------------------------------------
- // Description : Constructor
- //----------------------------------------------------------------------------------------
-
- GameMain :: GameMain()
- {
- //Initialize all variables and objects
- ConCurrent.New();
- TetCurrent.New(1, 1);
- TetNext.New(1, 1);
- Stats.Score = 0;
- Stats.Level = 0;
- Stats.Lines = 0;
- Drop = 0;
- Stats.TetTotal = 0;
-
- for (int n = 0; n < TET_MAX_TYPES; n++)
- Stats.TetTypeCount[n++] = 0;
-
- }// GameMain
-
-
-
- //----------------------------------------------------------------------------------------
- // Description : Initialize the Game eg. sets up the game window
- //----------------------------------------------------------------------------------------
-
- bool GameMain :: Init()
- {
- GameGL.SetFullScreen(true); // Fullscreen Mode
-
- // Create OpenGL Window 640 x 480 x 16
- if (!GameGL.CreateGLWindow( "GAME_NAME", 640, 480, 16, GameGL.GetFullScreen()))
- return false; // Quit if window was not created
-
- GameGL.InitGL(); // Init game specific GL defaults
-
- HighScores.Init();
-
- srand( (unsigned)time( NULL ) ); // Seed for random number gen based on timer
-
- return true; // Initialization was OK
-
- }// Init
-
-
-
-
- //----------------------------------------------------------------------------------------
- // Description : Reset Game Vars ie. start a new game
- //----------------------------------------------------------------------------------------
-
- void GameMain :: New()
- {
- Mode = GAME_START_SCREEN; //
- GameOver = false;
- TetCurrent.New(Random(), 1); // Init a random Tetramino (Current "Active")
- TetNext.New(Random(), 1); // Init a random Tetramino (Next)
-
- ConCurrent.New(); // Init a New Container
- ConCurrent.PlaceTet(TetCurrent); // Place the new tetramino onto the Container
-
- Speed = 1000; // Reset the falling speed
- Drop = false; // No tetranimoes being droped
- DropHeight = 0; // Not dropped ie. zero height
-
- Stats.Score = 0; // Reset the score
- Stats.Level = 1; // Reset the level
- Stats.Lines = 0; // Reset the Line couter
- Stats.TetTotal = 0; // Zero tetraminoes used in play
- for (int n = 0; n < TET_MAX_TYPES; n++)
- Stats.TetTypeCount[n] = 0; // Init individual tetramino type counters
-
- }// New
-
-
-
-
- //----------------------------------------------------------------------------------------
- // Description : Calculates new score based on defined values and object vars
- //----------------------------------------------------------------------------------------
-
- void GameMain :: SetScore(char v_NoOfLines)
- {
- int TmpVal = 0;
-
- switch(v_NoOfLines) // How many lines removed with a tetramino
- {
- case 1 : { TmpVal = SCR_ONE_LINE_REMOVED; break;}
- case 2 : { TmpVal = SCR_TWO_LINES_REMOVED; break;}
- case 3 : { TmpVal = SCR_THREE_LINES_REMOVED; break;}
- case 4 : { TmpVal = SCR_FOUR_LINES_REMOVED; break;}
- default : TmpVal = 1;
- }//switch
-
- TmpVal += Stats.Level * SCR_TETRAMINO_DROPPED * TmpVal + DropHeight; // Bonus points for dropping
- Stats.Score += TmpVal; // Adjust the current score
-
- DropHeight = 0;
-
- }// SetScore
-
-
-
- //----------------------------------------------------------------------------------------
- // Description : Displays main startup screen
- //----------------------------------------------------------------------------------------
-
- void GameMain :: StartScreen(unsigned char v_UserAction)
- {
- if(v_UserAction == 32)
- Mode = GAME_ACTIVE;
-
- if(v_UserAction == 'S')
- StarFieldActive = !StarFieldActive;
-
- if(v_UserAction == 'M')
- SoundActive = !SoundActive;
-
- GameGL.RenderStart();
- GameGL.RenderStarField();
- GameGL.RenderStartScreen(HighScores);
- GameGL.RenderEnd();
-
- SwapBuffers(GameGL.GethDC()); // Swap Buffers (Double Buffering)
-
- }//end StartScreen
-
-
-
- //----------------------------------------------------------------------------------------
- // Description : verifies user text input
- //----------------------------------------------------------------------------------------
-
- bool GameMain :: ValidInput(unsigned char Val)
- {
- static bool ValidStatus;
- ValidStatus = false;
- static char ValidChars[] = "0123456789abcdefghijklmonpqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
-
- for(int n = 0; ValidChars[n] != '\0'; n++)
- if(ValidChars[n] == Val)
- ValidStatus = true;
-
- return ValidStatus;
-
- }//end ValidInput
-
-
- //----------------------------------------------------------------------------------------
- // Description : Allows player to enter thier names
- //----------------------------------------------------------------------------------------
-
- void GameMain :: GetHighScore(unsigned char v_UserAction)
- {
- static char Name[25] = ".......................";
- static char n = 0;
-
- //Enter
- if(v_UserAction == 13)
- {
- Mode = GAME_START_SCREEN;
- //Save the Score
- HighScores.SetEntry(Name, Stats.Score, Stats.Lines, Stats.Level);
- }//if
-
-
- //Backspace
- if(v_UserAction == 8)
- {
- if(n > 0)
- {
- Name[n - 1] = '.';
- n--;
- }//if
- }//if
-
- if(v_UserAction == ' ')
- {
- Name[n] = '.';
- n++;
- }//if
-
- if((n < 23) && v_UserAction)
- {
- if(ValidInput(v_UserAction))
- {
- Name[n] = v_UserAction;
- n++;
- }
- }//if
-
- if(n < 0) n = 0;
-
- GameGL.RenderStart();
- GameGL.RenderStarField();
- GameGL.RenderGameTitle(-1.0f, 1.5f);
- GameGL.RenderHighScoreScreen(Name, n, Stats.Score);
- //GameGL.DebugVal(v_UserAction);
- GameGL.RenderEnd();
-
- SwapBuffers(GameGL.GethDC()); // Swap Buffers (Double Buffering)
-
- }//end
-
-
-
-
- //----------------------------------------------------------------------------------------
- // Description : Main gameplay sequence ie. the Gameloop
- //----------------------------------------------------------------------------------------
-
- void GameMain :: Play(unsigned char v_UserAction)
- {
- if(GameOver)
- {
- ConCurrent.SetAction(CON_GAME_OVER);
- if(v_UserAction != 0)
- {
- if(HighScores.IsNewHighScore(Stats.Score))
- Mode = GAME_NEW_HIGHSCORE;
- else
- Mode = GAME_START_SCREEN;
- }//if
- v_UserAction = 0;
-
- }//if
-
- //incase numlock is on
- if (v_UserAction == VK_NUMPAD4) v_UserAction = PLR_MOVE_LEFT;
- if (v_UserAction == VK_NUMPAD6) v_UserAction = PLR_MOVE_RIGHT;
- if (v_UserAction == VK_NUMPAD2) v_UserAction = PLR_MOVE_DOWN;
- if (v_UserAction == VK_NUMPAD0) v_UserAction = PLR_MOVE_DROP;
-
- if(Drop || TetCurrent.DropToNextRow())
- v_UserAction = PLR_MOVE_DOWN; // A tetramino is being dropped by user or timer
-
- if (ConCurrent.GetAction() == CON_NONE) // Use user input only if no actions are active
- {
- switch (v_UserAction) // What user action was performed
- {
- case PLR_MOVE_LEFT: // MOVE LEFT
- {
- GameGL.RenderRotationArrows(v_UserAction); // Show Left rotation arrow
- ConCurrent.RemoveTet(TetCurrent); // Remove Tetramino from Container
- TetCurrent.MoveLeft(); // Move Tetramino to the left
- if (ConCurrent.PlaceTet(TetCurrent)) // Place the Tetramino & Check for collision
- ConCurrent.SetAction(CON_ROTATE_RIGHT); // Tell container to rotate to the right
- else{
- TetCurrent.MoveRight(); // If Yes, Move Tetramino back to old position
- ConCurrent.PlaceTet(TetCurrent); // Place the Tetramino
- }//if
- break;
- }//case
-
- case PLR_MOVE_RIGHT: // MOVE RIGHT
- {
- GameGL.RenderRotationArrows(v_UserAction); // Show Right rotation arrow
- ConCurrent.RemoveTet(TetCurrent);
- TetCurrent.MoveRight();
- if (ConCurrent.PlaceTet(TetCurrent))
- ConCurrent.SetAction(CON_ROTATE_LEFT);
- else{
- TetCurrent.MoveLeft();
- ConCurrent.PlaceTet(TetCurrent);
- }//if
- break;
- }//case
-
- case PLR_MOVE_DOWN: // MOVE DOWN
- {
- ConCurrent.RemoveTet(TetCurrent);
- TetCurrent.MoveDown();
- if (!ConCurrent.PlaceTet(TetCurrent))
- {
- TetCurrent.MoveUp();
- ConCurrent.PlaceTet(TetCurrent);
- if(SoundActive) PlaySound("data/blip.wav", NULL, SND_ASYNC);// Play sound
- Drop = 0; // Is no longer being dropped
- ConCurrent.SetAction(CON_CHECK_FOR_LINES); // Tell container to check for lines
- }//if
- break;
- }//case
-
- case PLR_MOVE_DROP: // DROP
- {
- Drop = true; // Set drop state to active (true)
- DropStarty = TetCurrent.y; //
- DropHeight = 17 - TetCurrent.y; // Calc Hieght of drop
- break;
- }//case
-
- case PLR_ROTATE_RIGHT: // ROTATE RIGHT
- {
- ConCurrent.RemoveTet(TetCurrent);
- TetCurrent.RotateRight();
- if (!ConCurrent.PlaceTet(TetCurrent))
- {
- TetCurrent.RotateLeft();
- ConCurrent.PlaceTet(TetCurrent);
- }//if
- break;
- }//case
-
- case PLR_ROTATE_LEFT: // ROTATE LEFT
- {
- ConCurrent.RemoveTet(TetCurrent);
- TetCurrent.RotateLeft();
- if (!ConCurrent.PlaceTet(TetCurrent))
- {
- TetCurrent.RotateRight();
- ConCurrent.PlaceTet(TetCurrent);
- }//if
- break;
- }//case
-
- case 'S': // Turn starfield on/off
- {
- StarFieldActive = !StarFieldActive;
- break;
- }//case
-
- case 'M': // Turn starfield on/off
- {
- SoundActive = !SoundActive;
- break;
- }//case
-
- }//switch
- }//if
-
- ConCurrent.DoAction(); // Allow container to perform its current active actions
-
- if((ConCurrent.GetAction() == CON_START_NEXT) && !GameOver) // If Containers current action is Start Next
- {
- //Check for GameOver condition
- if(TetCurrent.y <= 0)
- {
- GameOver = true;
- GameGL.RenderGameOver("New");
- }//if
-
- ConCurrent.SetAction(CON_NONE); // Action performed, No more actions
- SetScore(Stats.Lines += ConCurrent.GetNoOfLinesFound()); // Update the score and inc line total
- ConCurrent.ResetNoOfLinesFound(); // Reset current line count val ie. zero
-
- //Go to next level
- if(Stats.Lines > (Stats.Level * 10))
- Stats.Level++;
-
- if(!GameOver)
- StartNext(); // Place the Next tetramino onto the container
-
- }//if
-
- // Render all game elements to screen
- Render();
-
- }// Play
-
-
-
-
-
- //----------------------------------------------------------------------------------------
- // Description : Sets NEXT piece to CURRENT and inits another NEXT piece
- //----------------------------------------------------------------------------------------
-
- void GameMain :: StartNext()
- {
- Stats.TetTypeCount[TetCurrent.Type - 1]++; // Increment the appropriate tet count
- Stats.TetTotal++; // Increment tatal for tetraminos
- char tmp_x = TetCurrent.x; // Remebers prev x position in container
- TetCurrent.New(TetNext.Type, Stats.Level); // Sets the TetCurrent = TetNext
- TetCurrent.x = tmp_x; // Position it to the current x pos
- TetNext.New(Random(), Stats.Level); // Create a new TetNext
- ConCurrent.PlaceTet(TetCurrent); // Place the new tetra in the container
-
- }// StartNext
-
-
-
-
-
-
-
- //----------------------------------------------------------------------------------------
- // Description : Render the game on the screen
- //----------------------------------------------------------------------------------------
-
- void GameMain :: Render()
- {
- GameGL.RenderStart();
- GameGL.RenderStarField();
- GameGL.RenderGameTitle(-5.0f, -4.0f);
- GameGL.RenderGameStats(Stats); // Score etc.
- GameGL.RenderContainer(ConCurrent); // Container
- GameGL.RenderNextPreview(TetNext);
- GameGL.RenderRotationArrows(0);
- GameGL.RenderDropArrow(DropStarty, TetCurrent.y, Drop);
- if(GameOver) GameGL.RenderGameOver("");
- GameGL.RenderEnd();
-
- SwapBuffers(GameGL.GethDC()); // Swap Buffers (Double Buffering)
-
- }// Render
-
-
-
-
-
- //----------------------------------------------------------------------------------------
- // Description : Shutdown
- //----------------------------------------------------------------------------------------
-
- void GameMain :: Quit()
- {
-
- GameGL.KillGLWindow();
-
- }//End Show
-
-
-
-
-
-
- //----------------------------------------------------------------------------------------
- // Description : For generating random tetraminos
- //----------------------------------------------------------------------------------------
-
- char GameMain :: Random()
- {
- double rn = 0;
-
- rn = ((double)rand() / (double)(RAND_MAX + 1)); // random float value in range [0,1)
-
- rn = (char)(rn * TET_MAX_TYPES);
-
- return (char)rn + 1;
-
- }//End Random